//publish 版本
import fs from 'fs'
import path from 'path'
import readline from 'readline'
import {execSync} from 'child_process'
import chalk from 'chalk'
const packageJsonPath = path.join(process.cwd(), 'package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
let version = packageJson.version
const versionReadline = readline.createInterface({
	input: process.stdin,
	output: process.stdout
})
let newVersion = ((v) => {
	let varr = v.split('.')
	let vlast = varr[varr.length - 1]
	let vNew = parseInt(vlast) + 1
	varr.splice(varr.length - 1, 1, vNew)
	return varr.join('.')
})(version)
let smartFileUiPackage = path.join(process.cwd(), '../smart-file-ui/package.json')
let smartFileUiPackageJson = JSON.parse(fs.readFileSync(smartFileUiPackage, 'utf8'))
let smartPortalUiPackage = path.join(process.cwd(), '../smart-portal-ui/package.json')
let smartPortalUiPackageJson = JSON.parse(fs.readFileSync(smartPortalUiPackage, 'utf8'))
let smartTimesheetUiPackage = path.join(process.cwd(), '../smart-timesheet-ui/package.json')
let smartTimesheetUiPackageJson = JSON.parse(fs.readFileSync(smartTimesheetUiPackage, 'utf8'))

function syncParentDependency(pkgPath, pkgJson, version) {
    const depKey = 'elead-components-v3';
    if (!pkgJson['dependencies']) pkgJson['dependencies'] = {};
    const before = pkgJson['dependencies'][depKey];
    console.log(`准备写入依赖：${pkgPath}`);
    console.log(`写入前依赖版本：${before}`);
    if (before !== version) {
        pkgJson['dependencies'][depKey] = version;
        fs.writeFileSync(pkgPath, JSON.stringify(pkgJson, null, 2));
        const after = JSON.parse(fs.readFileSync(pkgPath, 'utf8')).dependencies[depKey];
        console.log(chalk.cyan(`${path.basename(path.dirname(pkgPath))} 已同步 elead-components-v3@${version} 依赖。`));
        console.log(`写入后依赖版本：${after}`);
    } else {
        console.log(chalk.yellow(`${path.basename(path.dirname(pkgPath))} 依赖已是 elead-components-v3@${version}，无需更新。`));
    }
}

versionReadline.question(`当前版本 ${version} , 是否要升级为 : ${newVersion} (y/n)`, (newName) => {
	versionReadline.close()
	if (newName.toLowerCase() == 'y') {
		packageJson.version = newVersion
		fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
		execSync('npm publish', {stdio: ['inherit', 'inherit', 'inherit']})
		console.log('publish success')
		syncParentDependency(smartFileUiPackage, smartFileUiPackageJson, newVersion)
		syncParentDependency(smartPortalUiPackage, smartPortalUiPackageJson, newVersion)
		syncParentDependency(smartTimesheetUiPackage, smartTimesheetUiPackageJson, newVersion)
	} else {
		//输入新的版本号
		const inputVersionReadline = readline.createInterface({
			input: process.stdin,
			output: process.stdout
		})
		inputVersionReadline.question('输入版本号：', (inputVersion) => {
			inputVersionReadline.close()
			packageJson.version = inputVersion
			fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
			execSync('npm publish', {stdio: ['inherit', 'inherit', 'inherit']})
			console.log(chalk.green('发布成功 , 当前版本是 : ' + inputVersion))
			syncParentDependency(smartFileUiPackage, smartFileUiPackageJson, inputVersion)
			syncParentDependency(smartPortalUiPackage, smartPortalUiPackageJson, inputVersion)
			syncParentDependency(smartTimesheetUiPackage, smartTimesheetUiPackageJson, inputVersion)
		})
	}
})
